package com.base.monitor.module.biz.context;

import cn.hutool.core.collection.CollectionUtil;
import com.base.monitor.model.model.biz.cfg.entity.Gateway;
import com.base.monitor.model.model.biz.cfg.entity.Indicator;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 设备信息上下文
 */
public class CfgContext {

    // 网关数据
    private static final List<Gateway> gatewayVoList = Collections.synchronizedList(new ArrayList<>());

    // 指标数据
    private static final List<Indicator> indicatorAllVoList = Collections.synchronizedList(new ArrayList<>());

    // 网关在线时间<id, date>
    private static final Map<Long, Date> gatewayOnlineMap = new ConcurrentHashMap<>();

    // 指标在线时间<id, date>
    private static final Map<String, Date> indicatorOnlineMap = new ConcurrentHashMap<>();

    /**
     * 添加网关信息
     * @param gateway 网关信息
     */
    public static void addGatewayVoList(Gateway gateway) {
        gatewayVoList.add(gateway);
    }

    /**
     * 批量添加网关信息
     * @param gatewayVos 网关信息的集合
     */
    public static void addGatewayVoList(List<Gateway> gatewayVos) {
        gatewayVoList.addAll(gatewayVos);
    }

    /**
     * 获取网关信息
     * @return
     */
    public static List<Gateway> getGatewayVoList() {
        return gatewayVoList;
    }

    /**
     * 根据连接命令获取网关信息
     * @param command 连接命令
     * @return
     */
    public static Gateway getGatewayVoByCommand(String command) {
        List<Gateway> result = CfgContext.getGatewayVoList().stream()
                .filter(item -> item.getCommand().equals(command))
                .collect(Collectors.toList());

        if (CollectionUtil.isEmpty(result)) {
            return null;
        }
        return result.get(0);
    }

    /**
     * 清除网关信息
     */
    public static void clearGatewayVoList() {
        gatewayVoList.clear();
    }

    /**
     * 添加指标信息
     * @param indicator 指标信息
     */
    public static void addIndicatorAllVoList(Indicator indicator) {
        indicatorAllVoList.add(indicator);
    }

    /**
     * 批量添加指标信息
     * @param indicatorAllVos 指标信息的集合
     */
    public static void addIndicatorAllVoList(List<Indicator> indicatorAllVos) {
        indicatorAllVoList.addAll(indicatorAllVos);
    }

    /**
     * 获取指标信息
     * @return
     */
    public static List<Indicator> getIndicatorAllVoList() {
        return indicatorAllVoList;
    }

    /**
     * 根据网关id查询指标信息
     * @param id 网关id
     * @return
     */
    public static List<Indicator> getIndicatorAllVoListByGatewayId(Long id) {
        if (CollectionUtil.isEmpty(indicatorAllVoList)) {
            return null;
        }
        return indicatorAllVoList.stream()
                .filter(item -> item.getGatewayId().equals(id))
                .collect(Collectors.toList());
    }

    /**
     * 清除指标信息
     */
    public static void clearIndicatorAllVoList() {
        indicatorAllVoList.clear();
    }

    /**
     * 保存网关在线时间
     * @param gatewayId 网关id
     */
    public static void addGatewayOnlineTime(Long gatewayId) {
        gatewayOnlineMap.put(gatewayId, new Date());
    }

    /**
     * 获取网关在线时间
     * @return
     */
    public static Map<Long, Date> getGatewayOnlineMap() {
        return gatewayOnlineMap;
    }

    /**
     * 清空网关在线时间
     */
    public static void clearGatewayOnlineTime() {
        gatewayOnlineMap.clear();
    }

    /**
     * 保存指标在线时间
     * @param indicatorId 指标id
     */
    public static void addIndicatorOnlineTime(String indicatorId) {
        indicatorOnlineMap.put(indicatorId, new Date());
    }

    /**
     * 获取指标在线时间
     * @return
     */
    public static Map<String, Date> getIndicatorOnlineMap() {
        return indicatorOnlineMap;
    }

    /**
     * 清空指标在线时间
     */
    public static void clearIndicatorOnlineTime() {
        indicatorOnlineMap.clear();
    }
}
